from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    """扩展用户模型"""
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='手机号', db_index=True)
    avatar = models.URLField(blank=True, null=True, verbose_name='头像')
    is_active = models.BooleanField(default=True, verbose_name='是否激活', db_index=True)
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_index=True)
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    # 使用Django内置的ManyToMany关系
    roles = models.ManyToManyField('Role', blank=True, verbose_name='角色', related_name='users')

    class Meta:
        db_table = 'auth_user'
        verbose_name = '用户'
        verbose_name_plural = '用户'
        indexes = [
            models.Index(fields=['username', 'is_active']),
            models.Index(fields=['email', 'is_active']),
        ]


class Permission(models.Model):
    """权限模型"""
    name = models.CharField(max_length=100, verbose_name='权限名称')
    code = models.CharField(max_length=100, unique=True, verbose_name='权限代码')
    description = models.TextField(blank=True, null=True, verbose_name='权限描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'rbac_permission'
        verbose_name = '权限'
        verbose_name_plural = '权限'

    def __str__(self):
        return self.name


class Role(models.Model):
    """角色模型"""
    name = models.CharField(max_length=100, verbose_name='角色名称')
    code = models.CharField(max_length=100, unique=True, verbose_name='角色代码')
    description = models.TextField(blank=True, null=True, verbose_name='角色描述')
    permissions = models.ManyToManyField(Permission, blank=True, verbose_name='权限')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'rbac_role'
        verbose_name = '角色'
        verbose_name_plural = '角色'

    def __str__(self):
        return self.name





class Menu(models.Model):
    """菜单模型"""
    name = models.CharField(max_length=100, verbose_name='菜单名称')
    path = models.CharField(max_length=200, verbose_name='菜单路径')
    component = models.CharField(max_length=200, blank=True, null=True, verbose_name='组件路径')
    icon = models.CharField(max_length=100, blank=True, null=True, verbose_name='图标')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True, verbose_name='父菜单')
    sort_order = models.IntegerField(default=0, verbose_name='排序')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    permissions = models.ManyToManyField(Permission, blank=True, verbose_name='所需权限')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'rbac_menu'
        verbose_name = '菜单'
        verbose_name_plural = '菜单'
        ordering = ['sort_order']

    def __str__(self):
        return self.name
